package cn.maxhou.controller;

import cn.max.anno.RequestMapping;
import cn.maxhou.entity.Auction;
import cn.maxhou.entity.User;
import cn.maxhou.service.AuctionService;
import cn.maxhou.service.AuctionrecordService;
import cn.maxhou.service.impl.AuctionServiceImpl;
import cn.maxhou.service.impl.AuctionrecordServiceImpl;
import cn.maxhou.util.RequestUtil;
import cn.maxhou.vo.Auctionrecord;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class AuctionController {

    AuctionService auctionService = new AuctionServiceImpl();

    @RequestMapping(value = "/getAuctionList")
    public String getAuctionList(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        List<Auction> auctionList = auctionService.getAuctionList();
        req.setAttribute("auctionList", auctionList);
        User user = (User) req.getSession().getAttribute("user");
        if (user.getUserIsAdmin()==0){
            return "forward:/pmplist.jsp";
        }else {
            return "forward:/pmplistadmin.jsp";
        }

    }

    @RequestMapping(value = "/searchAuctionList")
    public String searchAuctionList(HttpServletRequest req, HttpServletResponse resp) throws ParseException, UnsupportedEncodingException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        String auctionName= req.getParameter("auctionName");
        double auctionStartPrice=0.00;
        if (!req.getParameter("auctionStartPrice").isEmpty()) {
            auctionStartPrice=Double.parseDouble(req.getParameter("auctionStartPrice"));
        }
        String auctionStartTime= req.getParameter("auctionStartTime");
        String auctionEndTime= req.getParameter("auctionEndTime");
        String auctionDesc= req.getParameter("auctionDesc");

        Auction auction = new Auction();
        //SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");

        auction.setAuctionName(auctionName);
        auction.setAuctionStartTime(auctionStartTime);
        auction.setAuctionEndTime(auctionEndTime);
        auction.setAuctionDesc(auctionDesc);
        auction.setAuctionStartPrice(auctionStartPrice);

        AuctionService auctionService = new AuctionServiceImpl();

        List<Auction> auctionList = auctionService.searchAuctionList(auction);
        req.setAttribute("auctionList", auctionList);

        User user = (User) req.getSession().getAttribute("user");
        if (user.getUserIsAdmin()==0){
            return "forward:/pmplist.jsp";
        }else {
            return "forward:/pmplistadmin.jsp";
        }
    }

    @RequestMapping(value = "/deleteAuction")
    public void deleteAuction(HttpServletRequest req, HttpServletResponse resp) throws ParseException, IOException {

        PrintWriter out = resp.getWriter();

        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        int auctionId = Integer.parseInt(req.getParameter("auctionId"));

        int flag=3;

        try{
          flag  = auctionService.deleteAuction(auctionId);
        }catch (RuntimeException e){
            flag=0;
            e.printStackTrace();
        }

        out.println(flag);
    }

    @RequestMapping(value = "/getAuctionDetails")
    public String getAuctionDetails(HttpServletRequest req, HttpServletResponse resp)throws ParseException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

         int auctionId =Integer.parseInt(req.getParameter("auctionId"));

        AuctionService auctionService = new AuctionServiceImpl();
        AuctionrecordService auctionrecordService = new AuctionrecordServiceImpl();

        Auction auction = auctionService.getAuctionById(auctionId);
        List<Auctionrecord>  auctionrecord = auctionrecordService.getAuctionrecordById(auctionId);

        req.setAttribute("auction", auction);
        req.setAttribute("auctionrecord", auctionrecord);
        return "forward:/jp.jsp";
    }
}
